#pragma once

#include <iostream>
#include <fstream>
#include <unordered_map>
#include <string>
#include "log.hpp"

extern Log lg;

const std::string dicname = "./dict.txt";
const std::string seq = ":";

static bool Split(const std::string& str, std::string* part1, std::string* part2)
{
    auto pos = str.find(seq);
    if(pos != std::string::npos)
    {
        *part1 = str.substr(0, pos);
        *part2 = str.substr(pos+1);
        return true;
    }
    else
    {
        return false;
    }
}

class Init
{
public:
    Init()
    {
        std::ifstream ifs(dicname);
        if (!ifs.is_open())
        {
            lg(Fatal, "open %s file error", dicname.c_str());
            exit(1);
        }
        std::string line;
        while(std::getline(ifs, line))
        {
            std::string part1, part2;
            Split(line, &part1, &part2);
            dict.insert({part1, part2});
        }
        ifs.close();
    }

    std::string Translate(const std::string& word)
    {
        auto it = dict.find(word);
        if(it != dict.end())
        {
            return it->second;
        }
        else
        {
            return "Not Found!";
        }
    }
private:
    std::unordered_map<std::string, std::string> dict;
};